package com.ziyun.erp.modules.production.operate.test;

import com.ziyun.erp.common.e.order.ProductionStatusEnum;
import com.ziyun.erp.common.e.production.OrderProductionStatusEnum;
import com.ziyun.erp.modules.order.service.ErpOrderLogService;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.production.entity.ErpOrderProductionEntity;
import com.ziyun.erp.modules.production.service.ErpOrderProductionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guizhao
 * @Title:  生产中 状态
 * @ProjectName ziyun-service-erp
 * @date 2018-09-07 12:27
 */
public class ProducingStateAction implements  ProductionState{
    /**
     * 日志
     */
    private  static Logger logger = LoggerFactory.getLogger(ProducingStateAction.class);

    @Autowired
    private ErpOrderProductionService erpOrderProductionService;
    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private ErpOrderLogService erpOrderLogService;

    /**
     *  生产中 --> 生产完成
     * @param productionContext
     */
    @Override
    public boolean forward(ProductionContext productionContext) {
        ErpOrderProductionEntity erpOrderProductionEntity = productionContext.getErpOrderProductionEntity();
        if(erpOrderProductionEntity.getProductionStatus() != OrderProductionStatusEnum.PRODUCING.toInt()){
            return false;
        }

        Map<String,Object> updateMap = new HashMap<>();
        updateMap.put("productionStatus",OrderProductionStatusEnum.FINISH.toInt());
        updateMap.put("updateName",erpOrderProductionEntity.getUpdateName());
        erpOrderProductionService.updateByMap(updateMap,erpOrderProductionEntity.getProductionId());

        // 判断关联订单是否都生产完成
        boolean isOver = true;
        List<ErpOrderProductionEntity> otherList = erpOrderProductionService.queryOtherOrderProduction(
                erpOrderProductionEntity.getOrderNo(),erpOrderProductionEntity.getProductionId());
        if(otherList != null){
            for (ErpOrderProductionEntity checkProduction:otherList){
                if(checkProduction.getProductionStatus() != OrderProductionStatusEnum.FINISH.toInt()){
                    isOver = false;
                    break;
                }
            }
        }
        // 订单置未生产完成
        if(isOver){
            Map<String,Object> erpOrderMap = new HashMap<>();
            erpOrderMap.put("productionStatus",ProductionStatusEnum.PRODUCTION_FINISH.toInt());
            erpOrderService.updateByMap(erpOrderMap,erpOrderProductionEntity.getOrderNo());
            // 订单日志
            erpOrderLogService.insertOrderLog(erpOrderProductionEntity.getOrderNo(),"订单生产完成(所有生产单生产完成)");
        }
        return true;
    }

    @Override
    public boolean rollback(ProductionContext productionContext) {
        logger.info("返回操作待完成......");
        return false;
    }

    @Override
    public int getState() {
        return OrderProductionStatusEnum.PRODUCING.toInt();
    }
}
